package au.com.handsunited.service.impl;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import au.com.handsunited.dao.EntityDAO;
import au.com.handsunited.dao.InvitationDAO;
import au.com.handsunited.dao.MessageDAO;
import au.com.handsunited.dao.OrganisationDAO;
import au.com.handsunited.exception.business.MethodNotImplementedException;
import au.com.handsunited.model.BaseEntity;
import au.com.handsunited.model.Organisation;
import au.com.handsunited.model.User;
import au.com.handsunited.model.invitations.Invitation;
import au.com.handsunited.model.relationship.RelationshipStatus;
import au.com.handsunited.model.relationship.Role;
import au.com.handsunited.service.OrganisationService;

/**
 *
 * @author arunda
 */
public class OrganisationServiceImpl extends EntityServiceImpl
		implements OrganisationService {


	private static Logger logger = LoggerFactory.getLogger(OrganisationServiceImpl.class);

	private final OrganisationDAO organisationDAO;


	/**
	 * @param organisationDAO
	 * @param messageDAO TODO
	 */
	public OrganisationServiceImpl(EntityDAO entityDAO, MessageDAO messageDAO, InvitationDAO invitationDAO, OrganisationDAO organisationDAO) {
		super(entityDAO, messageDAO, invitationDAO);
		this.organisationDAO = organisationDAO;
	}


	/**
	 * @see au.com.handsunited.service.OrganisationService#deleteOrganisation(au.com.handsunited.model.old.Organisation)
	 */
	public boolean deleteOrganisation(Organisation organisation) {
		throw new MethodNotImplementedException();
	}


	/**
	 * @see au.com.handsunited.service.OrganisationService#getOrganisation(String)
	 */
	public Organisation getOrganisation(String organisationId) {
		return getOrganisation(Long.parseLong(organisationId));
	}


	/**
	 * @see au.com.handsunited.service.OrganisationService#getOrganisation(java.lang.Long)
	 */
	@Override
	public Organisation getOrganisation(Long organisationId) {
		return organisationDAO.getOrganisation(organisationId);
	}


	/**
	 * @see au.com.handsunited.service.OrganisationService#getAllOrganisations()
	 */
	public List<Organisation> getAllOrganisations() {
		return organisationDAO.getAllOrganisations();
	}


	/**
	 * @see au.com.handsunited.service.OrganisationService#updateOrganisation(au.com.handsunited.model.Organisation, au.com.handsunited.model.BaseEntity)
	 */
	public Organisation updateOrganisation(Organisation organisation, BaseEntity owner) {
		return (Organisation) updateEntity(organisation, owner);
	}


	/**
	 * @see au.com.handsunited.service.OrganisationService#addMemberToOrganisation(au.com.handsunited.model.old.Organisation, au.com.handsunited.model.old.User)
	 */
	public void addMemberToOrganisation (Organisation organisation, BaseEntity entity) {

		addMemberToNetwork(organisation, entity, Role.MEMBER, Role.UNKNOWN, RelationshipStatus.AWAITING_AUTHORISATION, RelationshipStatus.ACTION_PENDING);
	}


	/**
	 * @see au.com.handsunited.service.OrganisationService#removeMemberFromOrganisation(au.com.handsunited.model.Organisation, au.com.handsunited.model.BaseEntity)
	 */
	@Override
	public void removeMemberFromOrganisation(Organisation organisation,
			BaseEntity member) {

	}


	/**
	 * @see au.com.handsunited.service.OrganisationService#searchForOrganisationsByCriteria(Organisation)
	 */
	public List<Organisation> searchForOrganisationsByCriteria(Organisation organisationCriteria) {
		return organisationDAO.searchForOrganisationsByCriteria(organisationCriteria);
	}



	/**
	 * @see au.com.handsunited.service.OrganisationService#searchForOrganisationsByKeywords(java.lang.String)
	 */
	@Override
	public List<Organisation> searchForOrganisationsByKeywords(String keywords) {
		return organisationDAO.searchForOrganisationsByKeywords(keywords);
	}


	/**
	 * @see au.com.handsunited.service.OrganisationService#changeMemberRole(au.com.handsunited.model.old.Organisation, au.com.handsunited.model.old.User, au.com.handsunited.model.old.User, au.com.handsunited.model.old.Role)
	 */
	public void changeMemberRole(Organisation organisation, User admin, User member, Role newRole) {

	}


	/**
	 * @see au.com.handsunited.service.OrganisationService#createOrganisation(au.com.handsunited.model.Organisation, au.com.handsunited.model.BaseEntity, BaseEntity)
	 */
	public Organisation createOrganisation(Organisation organisation, BaseEntity owner, BaseEntity creator) {

		if ((owner != null) && (creator != null)) {
			organisation = (Organisation) createEntity(organisation, owner, creator);

			if (OrganisationServiceImpl.logger.isDebugEnabled()) {
				OrganisationServiceImpl.logger.debug("New Organisation [{}] created by {}[{}]",
						new String[] {organisation.getName(), owner.getEntityType(), owner.getName()});
			}
			return organisation;

		} else {
			return null;
		}
	}


	/**
	 * @see au.com.handsunited.service.OrganisationService#inviteOrganisation(User, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public void inviteOrganisation(User user, String organisationName,
			String contactName, String emailAddress, String message) {

		invitePotentialEntity(user, organisationName, emailAddress, contactName, Invitation.INVITEE_TYPE_ORGANISATION, message);
	}



	/**
	 * @see au.com.handsunited.service.OrganisationService#inviteToOrganisation(Organisation, User, List, java.lang.String, java.lang.String)
	 */
	@Override
	public Map<String, List<Invitation>> inviteToOrganisation(Organisation organisation, User invitingUser,
			List<BaseEntity> invitedUsers, String emailAddresses, String message) {

		return inviteTo(organisation, invitingUser, invitedUsers, emailAddresses, message);
	}

}
